/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.mojo.flex.generator;

import org.granite.generator.as3.As3Type;
import org.granite.generator.as3.reflect.JavaInterface;
import org.granite.generator.as3.reflect.JavaMethod.MethodType;
import org.granite.generator.as3.reflect.JavaTypeFactory;

import java.lang.reflect.Method;

import java.net.URL;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @author Izzet_Mustafayev
 *
 */
public class JavaInterfaceX extends JavaInterface {
    public Collection<JavaMethod> methods;
    public Set<String> methodImports = new HashSet<String>();
    protected final As3Type as3Type;
    private String packageName;

    public JavaInterfaceX(JavaTypeFactory provider, Class<?> type, URL url) {
        super(provider, type, url);
        methods = getMethods();
        as3Type = super.getAs3Type();
        packageName = as3Type.getPackageName();
    }

    public JavaInterfaceX(JavaTypeFactory provider, Class<?> type, String packageName, URL url) {
        this(provider, type, url);
        this.packageName = packageName;
    }

    @Override
    public As3Type getAs3Type() {
        As3Type tmpAS3Type = new As3Type(packageName, as3Type.getName(), as3Type.getNullValue());

        return tmpAS3Type;
    }

    private Collection<JavaMethod> getMethods() {
        List<JavaMethod> jMethods = new ArrayList<JavaMethod>();
        Method[] methods = type.getMethods();

        for (int i = 0; i < methods.length; i++) {
            JavaMethod method = new JavaMethod(methods[i], MethodType.OTHER);
            jMethods.add(method);
            methodImports.addAll(method.resolveImports());
        }

        return jMethods;
    }

    public boolean hasMethods() {
        return !methods.isEmpty();
    }
}
